package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    //注入
    //图片
    @Autowired
    private GraphInfoService graphInfoService;
    //配套
    @Autowired
    private RoomFacilityService roomFacilityService;
    //标签
    @Autowired
    private RoomLabelService roomLabelService;
    //支付方式
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    //租期
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    //属性
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    //房间mapper
    @Autowired
    private RoomInfoMapper roomInfoMapper;

    //公寓信息
    @Autowired
    private  ApartmentInfoMapper apartmentInfoMapper;
    //图片mapper
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    //属性mapper
    @Autowired
    private RoomAttrValueMapper roomAttrValueMapper;
    //配套mapper\
    @Autowired
    private RoomFacilityMapper roomFacilityMapper;
    //标签mapper
    @Autowired
    private RoomLabelMapper roomLabelMapper;
    //支付mapper
    @Autowired
    private RoomPaymentTypeMapper roomPaymentTypeMapper;
    //租期mapper
    @Autowired
    private RoomLeaseTermMapper roomLeaseTermMapper;

    @Override
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {
        //保存or更新
        boolean update = roomSubmitVo.getId() != null;
        saveOrUpdate(roomSubmitVo);
        //更新：先删除后保存
        //图片
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //1. 图片的item_id是否与房间ID相等
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,roomSubmitVo.getId());
        //图片的item_type是否等于枚举类型的房间为2：ItemType.ROOM
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
        //IService的remove方法
        graphInfoService.remove(graphInfoLambdaQueryWrapper);
        //配套
        LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomFacilityLambdaQueryWrapper.eq(RoomFacility::getFacilityId,roomSubmitVo.getId());
        roomFacilityService.remove(roomFacilityLambdaQueryWrapper);
        //标签
        LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper =new LambdaQueryWrapper<>();
        roomLabelLambdaQueryWrapper.eq(RoomLabel::getLabelId,roomSubmitVo.getId());
        roomLabelService.remove(roomLabelLambdaQueryWrapper);
        //支付方式
        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getPaymentTypeId,roomSubmitVo.getId());
        roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);
        //租期
        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getLeaseTermId,roomSubmitVo.getId());
        roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);
        //属性
        LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getAttrValueId,roomSubmitVo.getId());
        roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);

        //保存
        //图片保存
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)){
            List<GraphInfo> graphInfoList = new ArrayList<>(graphVoList.size());
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setId(roomSubmitVo.getId());
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }
        //配套
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)){
            List<RoomFacility> roomFacilityList = new ArrayList<>(facilityInfoIds.size());
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility roomFacility = RoomFacility.builder()
                        .facilityId(facilityInfoId)
                        .roomId(roomSubmitVo.getId())
                        .build();
                roomFacilityList.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomFacilityList);
        }
        //标签
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(labelInfoIds)){
            List<RoomLabel> roomLabelList = new ArrayList<>(labelInfoIds.size());
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel roomLabel = RoomLabel.builder()
                        .labelId(labelInfoId)
                        .roomId(roomSubmitVo.getId())
                        .build();
                roomLabelList.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabelList);
        }
        //支付方式
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(paymentTypeIds)){
            List<RoomPaymentType> roomPaymentTypeList = new ArrayList<>(paymentTypeIds.size());
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType roomPaymentType = RoomPaymentType.builder()
                        .paymentTypeId(paymentTypeId)
                        .roomId(roomSubmitVo.getId())
                        .build();
                roomPaymentTypeList.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        }
        //租期
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(leaseTermIds)){
            ArrayList<RoomLeaseTerm> roomLeaseTermArrayList = new ArrayList<>(leaseTermIds.size());
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder()
                        .leaseTermId(leaseTermId)
                        .roomId(roomSubmitVo.getId())
                        .build();
                roomLeaseTermArrayList.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(roomLeaseTermArrayList);
        }
        //属性
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(attrValueIds)) {
            List<RoomAttrValue> roomAttrValueList = new ArrayList<>(attrValueIds.size());
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue roomAttrValue = RoomAttrValue.builder().attrValueId(attrValueId).roomId(roomSubmitVo.getId()).build();
                roomAttrValueList.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(roomAttrValueList);
        }
    }

    @Override
    public void customPage(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        roomInfoMapper.customSelectPage(page,queryVo);
    }

    @Override
    public RoomDetailVo customDetail(Long id) {
        //根据房间id获取其详情信息
        RoomInfo roomInfo = getById(id);
        //查询所有公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        //房间ID获得图片的vo对象，
        List<GraphVo> graphVoList = graphInfoMapper.selectGraphVoList(id, ItemType.ROOM);
        //房间ID获得属性的vo对象，
        List<AttrValueVo> attrValueVoList = roomAttrValueMapper.customSelectList(id);
        //房间ID获得配套的vo对象，
        List<FacilityInfo> facilityInfoList = roomFacilityMapper.customSelectList(id);
        //房间ID获得标签的vo对象，
        List<LabelInfo> labelInfoList = roomLabelMapper.customSelectList(id);
        //房间ID获得支付方式的vo对象，
        List<PaymentType> paymentTypeList = roomPaymentTypeMapper.customSelectList(id);
        //房间ID获得租期的vo对象，
        List<LeaseTerm> leaseTermList = roomLeaseTermMapper.customSelectList(id);

        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        return roomDetailVo;
    }

    @Override
    public void customRemoveById(Long id) {

//        LambdaUpdateWrapper<RoomItemVo> roomItemVoLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
//        roomItemVoLambdaUpdateWrapper.eq(RoomItemVo::getId,id)
//                .in(RoomItemVo::getIsCheckIn, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING, LeaseStatus.RENEWING);
//
//        if (roomItemVoLambdaUpdateWrapper != null) {
//            throw  new LeaseException(ResultCodeEnum.ILLEGAL_REQUEST.getMessage(),ResultCodeEnum.ILLEGAL_REQUEST.getCode());
//        }
        //删除ID
        removeById(id);
        //删除与此ID所有有关的表数据
        //图片
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.ROOM);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);
        //属性
        LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId,id);
        roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);
        //配套
        LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId,id);
        roomFacilityService.remove(roomFacilityLambdaQueryWrapper);
        //标签
        LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId,id);
        roomLabelService.remove(roomLabelLambdaQueryWrapper);
        //支付方式
        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId,id);
        roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);
        //租期
        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId,id);
        roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);
    }


}




